package com.ruoyi.crm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.crm.domain.CrmAcceptanceSheet;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.CrmClearAccount;
import com.ruoyi.crm.service.ICrmClearAccountService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目结算表Controller
 *
 * @author ruoyi
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/crm/clear")
public class CrmClearAccountController extends BaseController {
    @Autowired
    private ICrmClearAccountService crmClearAccountService;

    /**
     * 查询项目结算表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CrmClearAccount crmClearAccount) {
        startPage();
        List<CrmClearAccount> list = crmClearAccountService.selectCrmClearAccountList(crmClearAccount);
        return getDataTable(list);
    }

    /**
     * 导出项目结算表列表
     */
    @Log(title = "项目结算表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmClearAccount crmClearAccount) {
        List<CrmClearAccount> list = crmClearAccountService.selectCrmClearAccountList(crmClearAccount);
        ExcelUtil<CrmClearAccount> util = new ExcelUtil<CrmClearAccount>(CrmClearAccount.class);
        util.exportExcel(response, list, "项目结算表数据");
    }

    /**
     * 获取项目结算表详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(crmClearAccountService.selectCrmClearAccountById(id));
    }

    /**
     * 获取项目结算表详细信息
     */
    @GetMapping(value = "getInfoByOrderId/{orderId}")
    public AjaxResult getInfoByOrderId(@PathVariable("orderId") String orderId) {
        return AjaxResult.success(crmClearAccountService.getInfoByOrderId(orderId));
    }

    /**
     * 新增项目结算表
     */
    @Log(title = "项目结算表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmClearAccount crmClearAccount) {
        return toAjax(crmClearAccountService.insertCrmClearAccount(crmClearAccount));
    }

    /**
     * 修改项目结算表
     */
    @Log(title = "项目结算表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmClearAccount crmClearAccount) {
        return toAjax(crmClearAccountService.updateCrmClearAccount(crmClearAccount));
    }

    /**
     * 删除项目结算表
     */
    @Log(title = "项目结算表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(crmClearAccountService.deleteCrmClearAccountByIds(ids));
    }

    /**
     * 当没有结算数据后，初始化结算表
     * @return
     */
    @GetMapping("orderClearInit/{orderId}")
    public AjaxResult orderClearInit(@PathVariable String orderId){
        return AjaxResult.success(crmClearAccountService.orderClearInit(orderId));
    }


    /**
     * 审核
     *
     * @param crmClearAccount
     * @return
     */
    @PostMapping("audit")
    public AjaxResult audit(@RequestBody CrmClearAccount crmClearAccount) {
        return AjaxResult.success(crmClearAccountService.audit(crmClearAccount));
    }
}
